class RegistrationsController < ApplicationController
  before_filter :authenticate!
  before_filter :protect_from_student, :except => [:show, :create, :destroy]
  before_filter :protect_from_instructor, :except => [:show]
  before_filter :correct_user, :only => [:show, :destroy]
  # GET /registrations
  # GET /registrations.json
  def index
    @registrations = Registration.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @registrations }
    end
  end

  # GET /registrations/1
  # GET /registrations/1.json
  def show
    @registration = Registration.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @registration }
    end
  end

  # GET /registrations/new
  # GET /registrations/new.json
  def new
    @registration = Registration.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @registration }
    end
  end

  # GET /registrations/1/edit
  def edit
    @registration = Registration.find(params[:id])
  end

  # POST /registrations
  # POST /registrations.json
  def create
    @registration = Registration.new
    @registration.timeslot = Timeslot.find(params[:timeslot_id])
    @registration.register_time = Time.now
    @registration.student = @current_user.usable

    respond_to do |format|
      if @registration.save
        format.html { redirect_to timeslot_registration_path(@registration.timeslot, @registration), notice: 'Registration was successfully created.' }
        format.json { render json: @registration, status: :created, location: @registration }
      else
        format.html { redirect_to actions_path, alert: 'Registration failed' }
        format.json { render json: @registration.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /registrations/1
  # PUT /registrations/1.json
  def update
    @registration = Registration.find(params[:id])

    respond_to do |format|
      if @registration.update_attributes(params[:registration])
        format.html { redirect_to @registration, notice: 'Registration was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @registration.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /registrations/1
  # DELETE /registrations/1.json
  def destroy
    @registration = Registration.find(params[:id])
    @registration.cancel_time = Time.now

    respond_to do |format|
			if @registration.save
      	format.html { redirect_to actions_url, notice: 'Registration cancelled' }
      	format.json { head :no_content }
			else
				format.html { redirect_to timeslot_registration_url(@registration.timeslot, @registration), alert: 'Cannot cancel closer to 24h from start time' }
      	format.json { head :no_content }
			end
		end
  end

  def correct_user
    @registration = Registration.find(params[:id])
    if @current_user.student?
      unless @current_user.usable.id == @registration.student.id
        redirect_to actions_url, :alert => "Not authorized to use this registration"
      end
    else
      unless @current_user.usable.id == @registration.timeslot.instructor.id
        redirect_to actions_url, :alert => "Not authorized to use this registration"
      end
    end
  end
end
